package com.wn.icloud.provider.uac.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.icloud.biz.BaseBiz;
import com.wn.icloud.common.msg.TableResultResponse;
import com.wn.icloud.common.vo.user.UserInfo;
import com.wn.icloud.provider.uac.entity.User;
import com.wn.icloud.provider.uac.mapper.MenuMapper;
import com.wn.icloud.provider.uac.mapper.UserMapper;
import com.wn.icloud.provider.uac.service.dto.PasswordResetDTO;
import com.wn.icloud.provider.uac.service.dto.UserByWorkTeamReqDTO;
import com.wn.icloud.provider.uac.vo.TeamNameAndPlineName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author leeyang
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserBiz extends BaseBiz<UserMapper, User> {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserMapper userMapper;


    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Override
    public void insertSelective(User entity) {
        String password = encoder.encode(entity.getPassword());
        entity.setPassword(password);
        super.insertSelective(entity);
    }

    @Override
//    @CacheClear(pre="user{1.username}")
    public void updateSelectiveById(User entity) {
        super.updateSelectiveById(entity);
    }

    /**
     * 根据用户名获取用户信息
     *
     * @param username
     * @return
     */
//    @Cache(key="user{1}")
    public User getUserByUsername(String username) {
        User user = new User();
        user.setUsername(username);
        return mapper.selectOne(user);
    }

    public void resetPassword(PasswordResetDTO passwordResetDTO) {
        User user = this.selectById(passwordResetDTO.getUserId());
        Assert.notNull(user, String.format("User not exits:[]", passwordResetDTO.getUserId()));
        String password = encoder.encode(passwordResetDTO.getNewPassword());
        user.setPassword(password);
        this.updateSelectiveById(user);
    }

    public void updatePassword(PasswordResetDTO passwordResetDTO) {
        User user = this.selectById(passwordResetDTO.getUserId());
        Assert.notNull(user, String.format("User not exits:[]", passwordResetDTO.getUserId()));
        // 检测密码
        Assert.isTrue(encoder.matches(passwordResetDTO.getOldPassword(), user.getPassword()), "旧密码错误");
        String password = encoder.encode(passwordResetDTO.getNewPassword());
        user.setPassword(password);
        this.updateSelectiveById(user);
    }


    public TableResultResponse getUserlist(Integer page,Integer size ){
        Example example = new Example(User.class);
        Page<Object> result = PageHelper.startPage(page, size);
        List<User> users = mapper.selectByExample(example);
        List<UserInfo> userInfos = new ArrayList<>();
        users.forEach(user -> {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId() + "");
            userInfo.setName(user.getName());
            userInfo.setDescription(user.getDescription());
            userInfo.setUsername(user.getUsername());
            userInfo.setSex(user.getSex());
            userInfo.setTelPhone(user.getTelPhone());
            userInfo.setMobilePhone(user.getMobilePhone());
            userInfos.add(userInfo);
        });
        return new TableResultResponse<UserInfo>(result.getTotal(), userInfos);
    }

    public UserInfo getUserInfo(Integer userid){
        User user = new User();
        user.setId(userid);
        User user1 = mapper.selectOne(user);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(user1.getId() + "");
        userInfo.setName(user1.getName());
        userInfo.setDescription(user1.getDescription());
        userInfo.setUsername(user1.getUsername());
        return userInfo;
    }


    public TableResultResponse<UserByWorkTeamReqDTO> getUserByWorkTeam(Long teamId, UserByWorkTeamReqDTO userByWorkTeamReqDTO, Integer page, Integer size){

        String userCode = userByWorkTeamReqDTO.getUserCode();
        String userName = userByWorkTeamReqDTO.getUserName();

        //根据当前班组id查询出班组下的所有人
        List<UserByWorkTeamReqDTO> list = userMapper.selectUserByWorkTeam(teamId,userCode,userName,page,size);
        //根据当前班组id查询班组名称和产线的名称
        TeamNameAndPlineName teamNameAndPlineName = userMapper.selectTeamNameAndPlineName(teamId);
        //封装
        list.forEach(userByWorkTeamReqDTO1 -> {
            userByWorkTeamReqDTO1.setTeamName(teamNameAndPlineName.getTeamName());
            userByWorkTeamReqDTO1.setPlineName(teamNameAndPlineName.getPlineName());
        });
        //返回
        return new TableResultResponse<UserByWorkTeamReqDTO>(Long.valueOf(list.size()),list);
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder1 = new BCryptPasswordEncoder(12);
        encoder1.encode("admin");
    }
}
